<?php

namespace App\Services\Game;


use App\Models\Game\Privacy;

class PrivacyService
{
    /**
     * @param $request
     * @return array
     */
    public function list($request){
        $searchParams = json_decode($request->get('searchParams'),true);
        $title = data_get($searchParams,'title');
        $page = $request->get('page',1);
        $pageSize = $request->get('limit',10);
        $model = Privacy::query();
        if ($title){
            $model->where('title','like',"%{$title}%");
        }
        $paginate = $model->orderBy('sorts','desc')
            ->orderBy('created_at','desc')->paginate($pageSize,'*','page',$page);
        $res = [];
        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['count'] = $paginate->total();
        $res['data'] = $paginate->items();

        $data = $paginate->items();
        $globalVariableRow = null;

        foreach ($data as $key => $item) {
            if ($item->noticeCilentType == 1) {
                $globalVariableRow = $item; // 保存这一行
                unset($data[$key]); // 从原数组中移除
                break; // 找到后退出循环
            }
        }

        // 如果找到，插入到第一行
        if ($globalVariableRow) {
            array_unshift($data, $globalVariableRow);
        }

        $res['data'] = $data;

        return $res;
    }

    public function getById($id){
        return  Privacy::query()->find($id);
    }


    /**
     * @param $request
     * @return array
     */
    public function listJs($request){
        $searchParams = json_decode($request->get('searchParams'),true);
        $title = data_get($searchParams,'title');
        $page = $request->get('page',1);
        $pageSize = $request->get('limit',10);
        $model = Privacy::query();
        if ($title){
            $model->where('title','like',"%{$title}%");
        }
        $paginate = $model->orderBy('sorts','desc')
            ->orderBy('created_at','desc')->paginate($pageSize,'*','page',$page);
        $res = [];
        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['count'] = $paginate->total();
        $res['data'] = $paginate->items();
        return $res;
    }

    public function getByIdJs($id){
        return  Privacy::query()->find($id);
    }

}
